﻿/*
·--------------------------------------------------------------------·
| TranslatorAddin - DocumentTranslator                               |
| Copyright (c) 2010, Ben Chege.                                     |
| http://code.google.com/p/translatoraddin                           |
·--------------------------------------------------------------------·
| This software is free software; you can redistribute it and/or     |
| modify it under the terms of the GNU Lesser General Public         |
| License as published by the Free Software Foundation; either       |
| version 2.1 of the License, or (at your option) any later version. |
|                                                                    |
| This software is distributed in the hope that it will be useful,   |
| but WITHOUT ANY WARRANTY; without even the implied warranty of     |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  |
| Lesser General Public License for more details.                    |
|                                                                    |
| GNU LGPL: http://www.gnu.org/copyleft/lesser.html                  |
·--------------------------------------------------------------------·
*/

namespace TranslatorAddin
{
    using Word = Microsoft.Office.Interop.Word;
    using Office = Microsoft.Office.Core;
    using Google.API;
    using Google.API.Translate;
    using Microsoft.Office.Core;
    using System.Reflection;
    using System.Windows.Documents;
    using System;
    using WordTranslatorAddin;

    public class DocumentTranslator
    {
        public static void TranslateDocument(Language from, Language to)
        {
            var word = Globals.ThisAddIn.Application;

            Word.Selection currentSelection = word.Selection;
            int start = currentSelection.Start;
            int end = currentSelection.End;

            if (start != end)
                translateSelection(currentSelection, from, to);
            else
                foreach (Word.Paragraph paragraph in word.ActiveDocument.Paragraphs)
                    translateParagraph(paragraph, from, to);
        }
             
        public delegate Language DetectLanguageDelegate();
      
        public static Language DetectLanguage()
        {
            var word = Globals.ThisAddIn.Application;
            Word.Selection currentSelection = word.Selection;
            object start = currentSelection.Start;
            object end = currentSelection.End;
            var googleTranslator = new TranslateClient("http://code.google.com");
            Language detectedlang = null;

            string text = word.ActiveDocument.Range(ref start, ref end).Text;
            if (text == null)
            {
                start = word.ActiveDocument.Content.Start;
                end = word.ActiveDocument.Content.End;
                text = word.ActiveDocument.Range(ref start, ref end).Text;
            }
            if (text != null)
                try
                {
                    bool isReliable;
                    double confidence;
                    detectedlang= (Language)googleTranslator.Detect(text, out isReliable, out confidence);
                    //googleTranslator.BeginDetect(text,
                    //        delegate(IAsyncResult result)
                    //        {
                    //            bool isReliable;
                    //            double confidence;
                    //            fromlang = (Language)googleTranslator.EndDetect(result, out isReliable, out confidence);
                    //            //cboSourceLanguage.Text = fromlang.ToString();
                    //        }, null);
                }
                catch (Exception ex)
                {
                    System.Windows.Forms.MessageBox.Show("Language Detection Failed " + ex.Message);
                }
            return detectedlang;
        }
        private static void translateParagraph(Word.Paragraph paragraph, Language from, Language to)
        {
            var googleTranslator = new TranslateClient("http://code.google.com");

            try
            {
                googleTranslator.BeginTranslate(paragraph.Range.Text,
                            from.Value,
                                to.Value,
                                    delegate(IAsyncResult result)
                                    {
                                        string translatedtext = googleTranslator.EndTranslate(result);
                                        paragraph.Range.InsertAfter(translatedtext);

                                    }, null);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Translation for paragraph Failed " + ex.Message);
            }
        }
        private static void translateSelection(Word.Selection currentSelection, Language from, Language to)
        {
            var googleTranslator = new TranslateClient("http://code.google.com");
            var word = Globals.ThisAddIn.Application;

            try
            {
                googleTranslator.BeginTranslate(currentSelection.Range.Text,
                            from.Value,
                                to.Value,
                                    delegate(IAsyncResult result)
                                    {
                                        string translatedtext = googleTranslator.EndTranslate(result);
                                        // Store the user's current Overtype selection
                                        bool userOvertype = word.Options.Overtype;

                                        // Make sure Overtype is turned off.
                                        if (word.Options.Overtype)
                                        {
                                            word.Options.Overtype = false;
                                        }

                                        // Test to see if selection is an insertion point.
                                        if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP)
                                        {
                                            currentSelection.TypeText(translatedtext);
                                            currentSelection.TypeParagraph();
                                        }
                                        else
                                            if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
                                            {
                                                // Move to start of selection.
                                                if (word.Options.ReplaceSelection)
                                                {
                                                    object direction = Word.WdCollapseDirection.wdCollapseStart;
                                                    currentSelection.Collapse(ref direction);
                                                }
                                                currentSelection.TypeText(translatedtext);
                                                currentSelection.TypeParagraph();
                                            }
                                            else
                                            {
                                                // Do nothing.
                                            }

                                        // Restore the user's Overtype selection
                                        word.Options.Overtype = userOvertype;

                                    }, null);
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show("Translation for paragraph Failed " + ex.Message);
            }
        }

    }
}
